from django.conf import settings
from django.conf.urls.defaults import *
from djwheel.langutils import merge_attrs
from treemenus.models import Menu, MenuItem
import appsettings

settings = merge_attrs(settings, appsettings)


def make_urlpatterns(cp_menu_item):
    urlpatterns = []
    for menu_item in MenuItem.objects.filter(named_url='').exclude(url='').all():
        url = menu_item.url

        try:
            contentpage = cp_menu_item.objects.get(menu_item=menu_item)
        except cp_menu_item.DoesNotExist:
            contentpage = cp_menu_item()
            contentpage.menu_item = menu_item
            contentpage.save()

        url = url.lstrip('/')
        urlpatterns += patterns('',
            (r'^%s$' % url, settings.CONTENTPAGES_DEFAULT_VIEW)
        )

    return urlpatterns